<template>
  <div class="sort">
    <!-- <span>排序：</span> -->
    <Sort name="阅读量" @sort="sortView" class="view"></Sort>
    <Sort name="下载量" @sort="sortDownload" class="download"></Sort>
  </div>
</template>

<script setup>
import Sort from './sort.vue'
let orderByView = []
let orderDirectView = []
let orderByDowload = []
let orderDirectDowload = []
let orderBy = []
let orderDirect = []
// 分页顺序,desc为降序，asc为升序
const sortMap = {
  0: '',
  1: 'asc',
  2: 'desc',
}
const emits = defineEmits(['sort'])

// 设置查看量sum(fl.read_num)
function sortView(val) {
  if (val) {
    orderByView = ['sum(fl.read_num)']
  } else {
    orderByView = []
  }
  orderDirectView = [sortMap[val]]
  setSort()
}

// 设置查看量sum(fl.download_num)
function sortDownload(val) {
  if (val) {
    orderByDowload = ['sum(fl.download_num)']
  } else {
    orderByDowload = []
  }
  orderDirectDowload = [sortMap[val]]
  setSort()
}

function setSort() {
  orderBy = orderByView.concat(orderByDowload)
  orderDirect = orderDirectView.concat(orderDirectDowload)
  orderBy = trimSpace(orderBy).join(',')
  orderDirect = trimSpace(orderDirect).join(',')
  emits('sort', { orderBy, orderDirect })
}

// 去掉数组中的空字符串
function trimSpace(array) {
  for (var i = 0; i < array.length; i++) {
    if (array[i] == '' || array[i] == null || typeof array[i] == 'undefined') {
      array.splice(i, 1)
      i = i - 1
    }
  }
  return array
}
</script>

<style scoped lang="scss">
.sort {
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #606266;
  font-weight: bold;
  .download {
    margin-left: 15px;
  }
}
</style>
